package com.eastjet.maintenance.contrller;

import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.*;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.service.impl.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备档案
 */
@RestController
@RequestMapping("/maintenance")
@CrossOrigin(origins = "*")
public class maintenanceController {
    @Autowired
    private EquipmentServiceImpl equipmentService;//设备档案service
    @Autowired
    private AnnexServiceImpl annexService;//附件service
    @Autowired
    private SubDevicesServiceImpl subDevicesService;//子设备service
    @Autowired
    private SImageServiceImpl sImageService;//照片service
    @Autowired
    private DocumentationServiceImpl documentationService;//文档service
    @Autowired
    private CodeDataFeing codeDataFeing;//自动生成代码

    /**
     * 新增设备档案
     *
     * @param eq
     * @return
     */
    @PostMapping("/InsertEquipment")
    public Result getEquipment(@RequestBody Map<String, Object> map) {
        Boolean b = equipmentService.InsertEquipment(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增设备档案成功" : "新增设备档案失败");
    }

    /**
     * 查询设备档案/条件查询
     *
     * @param eq
     * @return
     */

    @RequestMapping("/SelectEquipment")
    public Result SelectEquipment(Equipment eq, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Equipment> equipment = equipmentService.SelectAllEquipment(eq);
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipment);
        return new Result(pageInfo, equipment.isEmpty() ? Code.GET_ERR : Code.GET_OK, equipment.isEmpty() ? "查询设备档案失败" : "查询设备档案成功");
    }

    /**
     * 删除设备档案
     *
     * @param code
     * @return
     */
    @RequestMapping("/UpdateEquipment")
    public Result DeleteEquipment(String code) {
        Boolean b = equipmentService.DeleteEquipment(code);
        return new Result(b, b ? Code.DELETE_OK : Code.DELETE_ERR, b ? "删除设备档案成功" : "删除设备档案失败");
    }

    /**
     * 新增附件
     *
     * @param an
     * @return
     */
    @PostMapping("/InsertAnnex")
    public Result InsertAnnex(@RequestBody List<Annex> an) {
        for (Annex a : an) {
            a.setCode(codeDataFeing.test(new HashMap<String, String>() {{
                put("TableName", "equipment");
            }}));
        }
        Boolean b = annexService.InsertAnnex(an);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增附件成功" : "新增附件失败");
    }

    /**
     * 查询附件
     *
     * @param an
     * @return
     */
    @RequestMapping("/SelectAnnex")
    public Result SelectAnnex(@RequestParam("code") String code) {
        List<Annex> annexes = annexService.selectByMainId(code);
        return new Result(annexes, annexes.isEmpty() ? Code.GET_ERR : Code.GET_OK, annexes.isEmpty() ? "查询附件失败" : "查询附件成功");
    }

    /**
     * 上传照片
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
//        SImage sImage = new SImage();
//        sImage.setCode(codeDataFeing.test(new HashMap<String, String>() {{
//            put("TableName", code);
//        }}));
//        sImage.setPath(file.getOriginalFilename());
//        sImage.setCreatedBy("admin");
        if (file.isEmpty()) {
            return "Please select a file to upload.";
        }
        try {
            byte[] bytes = file.getBytes();
            String uploadDir = "C:/设备管理/device_management/ruoyi-ui/src/assets/images/";
            File uploadedFile = new File(uploadDir + file.getOriginalFilename());
            file.transferTo(uploadedFile);
//            sImageService.InsertImage(sImage);
            return "File uploaded successfully!";
        } catch (IOException e) {
            e.printStackTrace();
            return "File upload failed!";
        }
    }

    @RequestMapping("/SelectImg")
    public Result SelectImg(@RequestParam("code") String code) {
        List<SImage> sImages = sImageService.selectImageList(code);
        return new Result(sImages, sImages.isEmpty() ? Code.GET_ERR : Code.GET_OK, sImages.isEmpty() ? "查询照片失败" : "查询照片成功");
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/upload2")
    public String handleFileUpload2(@RequestParam("file") MultipartFile file) {
//        Documentation p = new Documentation();
//        p.setCode((codeDataFeing.test(new HashMap<String, String>() {{
//            put("TableName", code);
//        }})));
//        p.setFilename(file.getOriginalFilename());
//        p.setUploader("admin");
//        p.setSize(String.valueOf(file.getSize()));
//        p.setEid(1);
//        p.setTombstone(0);
        if (file.isEmpty()) {
            return "Please select a file to upload.";
        }
        try {
            byte[] bytes = file.getBytes();
            String uploadDir = "D:/upload/";
            File uploadedFile = new File(uploadDir + file.getOriginalFilename());
            file.transferTo(uploadedFile);
//            documentationService.InsertDocumentation(p);
            return "File uploaded successfully!";
        } catch (IOException e) {
            e.printStackTrace();
            return "File upload failed!";
        }
    }

    /**
     * 根据code编码查询文件
     *
     * @param code
     * @return
     */
    @RequestMapping("/SelectDocumentaion")
    public Result SelectDocumentaion(@RequestParam("code") String code) {
        System.out.println("文件code："+code);
        List<Documentation> documentations = documentationService.SelectDocumentation(code);
        System.out.println("查询结果："+documentations);
        return new Result(documentations, documentations.isEmpty() ? Code.GET_ERR : Code.GET_OK, documentations.isEmpty() ? "查询文件失败" : "查询文件成功");
    }

    /*
     * 新增子设备
     */
    @PostMapping("/InsertSubDevices")
    public Result InsertSubDevices(@RequestBody List<SubDevices> subDevices) {
        Boolean b = subDevicesService.InsertSubDevice(subDevices);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增子设备成功" : "新增子设备失败");
    }

    /*
     * 修改子设备
     */
    @PostMapping("/UpdateSubDevices")
    public Result UpdateSubDevices(@RequestBody List<SubDevices> subDevices) {
        Boolean b = subDevicesService.UpdateSubDevice(subDevices);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "修改子设备成功" : "修改子设备失败");
    }

    /**
     * 删除子设备
     *
     * @param code
     * @return
     */
    @RequestMapping("/DeleteSubDevices")
    public Result DeleteSubDevices(@RequestBody List<String> code) {
        Boolean b = subDevicesService.DeleteSubDevice(code);
        return new Result(b, b ? Code.DELETE_OK : Code.DELETE_ERR, b ? "删除子设备成功" : "删除子设备失败");
    }

    /**
     * 查询子设备
     *
     * @param code
     * @return
     */
    @RequestMapping("/SelectSubbDevices")
    public Result SelectSubbDevices(@RequestParam("code") String code) {
        List<SubDevices> subDevices = subDevicesService.selectSubDevicesByDeviceId(code);
        return new Result(subDevices, subDevices.isEmpty() ? Code.GET_ERR : Code.GET_OK, subDevices.isEmpty() ? "查询子设备失败" : "查询子设备成功");
    }
}